<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/dataAreaFunction.php");

$errMsg = "";
$dataArea = sxREQUEST('da');
if(sxREQUEST("save")) {
	$errMsg .= profDAUpd();
}
$sSQL = "SELECT data_area, description from data_area " .
			"WHERE data_area = '$dataArea' ";
//dbg_($sSQL);
$query = $objSDAO->query($sSQL);
$rowDA = $objSDAO->fetch();
$sSQL = "SELECT p.description, p.id_profile, pda.data_area, pda.permission " .
			"FROM profile p " .
			"LEFT OUTER JOIN data_area_permission pda " .
			"ON p.id_profile=pda.id_profile AND pda.data_area='$dataArea' " .
			"ORDER BY p.description";
//dbg_($sSQL);
$query = $objSDAO->query($sSQL);


//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var errMsg = "";
			var objFrm = document.getElementById("objFrm");
			var objMessArea =  document.getElementById("messArea");
			
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save new data area");
		}			
	}
</script>
<?php
echo(bodyHTML($aOpz));
//output comandi
$aCmd = array(	"save"=>array("pag"=>"javascript:objFrmSave();",
										"title"=>_t("Salva i dati"),
										"text"=>_t("salva")),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0))
					);
echo(commandsHTML($aCmd));	
//output dei dati
$numElem = 0;
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"dataAreaProfilesIns.php\" method=\"POST\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"da\" id=\"da\" value=\"$dataArea\">\n");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">\n");
echo("<tr>" .
		"<td class=\"titl01\" width=\"100%\" colspan='5'>"._t("Area dati").': '.
			$dataArea." - ".sxHE($rowDA['description'])."</td>" .
	"</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Non abilitato")."</td>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Solo visualizza")."</td>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Visualizza e modifica")."</td>" .
		"<td class=\"tabh01\" width=\"55%\">"._t("Profilo")."</td>" .
	"</tr>\n");
while($row = $objSDAO->fetch()){
	$chkProf = "";
	if(trim($row["data_area"]) != "" && trim($row["permission"]) == "N"){
		$chkProf = "CHECKED";
	}
	$chkWrite = "";
	if(trim($row["permission"]) != "" && trim($row["permission"]) == "M"){
		$chkWrite = "CHECKED";
	}
	$chkRead = "";
	if($chkProf == "" && $chkWrite == ""){
		$chkRead = "CHECKED";
	}
	$numElem ++;
	echo("<tr>" .
			"<td class=\"text01\" align='center'><input class=\"text01\" type=\"radio\" name=\"perm_".$row["id_profile"]."\" id=\"perm_".$row["id_profile"]."\" value=\"N\" $chkProf></td>" .
			"<td class=\"text01\" align='center'><input class=\"text01\" type=\"radio\" name=\"perm_".$row["id_profile"]."\" id=\"perm_".$row["id_profile"]."\" value=\"R\" $chkRead></td>" .
			"<td class=\"text01\" align='center'><input class=\"text01\" type=\"radio\" name=\"perm_".$row["id_profile"]."\" id=\"perm_".$row["id_profile"]."\" value=\"M\" $chkWrite></td>" .
			"<input type=\"hidden\" name=\"id_".$row["id_profile"]."\" id=\"id_".$row["id_profile"]."\" value=\"".$row["id_profile"]."\">" .
			"<td class=\"text01\">".$row["description"]."</td>" .
		"</tr>\n");
}
if($numElem == 0)
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"6\">"._t("Nessun profilo &egrave; definito nell'applicreqtype").".</td>".
		"</tr>\n");
echo("</form>");
echo("</table>\n");
echo(commandsHTML($aCmd));
echo(footerHTML());
$objSDAO->close();
?>
